import Vue from 'vue'
import Router from 'vue-router'
import login from '../pages/login.vue'
import homepage from '../pages/homepage.vue'
import discover from '../pages/discover.vue'
import usercenter from '../pages/usercenter.vue'
import navigation from '../pages/navigation.vue'
import postpage from '../pages/post.vue'
import browsetopic from '../pages/browsetopic.vue'
import communitycenter from '../pages/communitycenter.vue'
import login_temp from "@/pages/login_temp";





const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)


const routes = [
  {
    path: '',
    redirect: '/homepage',
  },

  {
    path: '/homepage',
    component: homepage,
    meta: {requiresAuth: true}
  },
  {
    path: '/discover',
    component: discover,
    meta: {requiresAuth: true}
  },
  {
    path: '/login',
    component: login,
    meta: {
      iflogin: true
    }
  },
  {
    path: '/login_temp',
    component: login_temp,
  },
  {
    path: '/usercenter',
    component: usercenter,
    meta: {requiresAuth: true}
  },
  {
    path: '/navigation',
    component: navigation,
    // meta: {requiresAuth: true}
  },
  {
    path: '/postpage',
    component: postpage,
    meta: {requiresAuth: true}
  },
  {
    path: '/browsetopic',
    component: browsetopic,
    meta: {requiresAuth: true}
  },
  {
    path: '/communitycenter',
    component: communitycenter,
    meta: {requiresAuth: true}
  },
  {
    path: '/searchpage',
    component: () => import('../pages/searchpage.vue'),
    meta: {requiresAuth: true}
  },
  {
    path: '/testpage',
    component: () => import('../pages/testpage.vue'),
    meta: {requiresAuth: true}
  },
  {
    path: '/chat',
    name: 'chat',
    component: () => import('@/pages/chat'),
    meta: {requiresAuth: true}
  },
  {
    path: '/newlogin',
    component:()=>import('@/pages/newlogin')
  },
  {
    path: '/tagpage',
    component:()=>import('@/pages/tagpage')
  }
]

const router = new Router({
  // mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  document.body.scrollTop = 0
  document.documentElement.scrollTop = 0;
  window.pageYOffset=0
  if (to.meta.requiresAuth == true) {
    console.log(window.localStorage.getItem("token"))
    if (window.localStorage.getItem("token") != undefined) {
      next()
    } else {
      router.push('/login')
    }
  }
  else {
    next()
  }
})

export default router
